//
//  LaunchGifViewtwo.m
//  safeLady
//
//  Created by xmy on 2016/12/14.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "LaunchGifViewtwo.h"
@interface LaunchGifViewtwo()<CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *handClick;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLayout;
@property (weak, nonatomic) IBOutlet UIImageView *boundImg;
@property (strong, nonatomic) CAAnimationGroup * boundGrounpAnimation;
@property (assign, nonatomic) BOOL animationOnce;

@end
@implementation LaunchGifViewtwo

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    [super awakeFromNib];
     [self.boundImg setHidden:YES];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([anim valueForKey:@"presentKey"]) {
        [self.handClick startAnimating];
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0/*延迟执行时间*/ * NSEC_PER_SEC));
        @weakify(self)
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            @strongify(self)
            [self.boundImg setHidden:NO];
            [self.boundImg.layer addAnimation:self.boundGrounpAnimation forKey:@"boundGrounpAnimation"];
        });
       
    }
    if ([anim valueForKey:@"boundGroupKey"]) {
        [self.boundImg.layer removeAllAnimations];
        [self.boundImg setHidden:YES];
    }
   
}
-(CAAnimationGroup *)boundGrounpAnimation{
    if(!_boundGrounpAnimation){
        CABasicAnimation *scaleAnima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
        scaleAnima.toValue = [NSNumber numberWithFloat:3.0f];
        scaleAnima.duration = 1.5f;
        scaleAnima.timingFunction = [CAMediaTimingFunction functionWithName:
                                     kCAMediaTimingFunctionLinear];
        CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
        opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
        opacityAnima.duration = 1.5f;
        _boundGrounpAnimation = [CAAnimationGroup animation];
        _boundGrounpAnimation.animations = [NSArray arrayWithObjects:scaleAnima,opacityAnima, nil];
        [_boundGrounpAnimation setValue:@"fi" forKey:@"boundGroupKey"];
        _boundGrounpAnimation.duration = 1.5f;
        _boundGrounpAnimation.delegate =self;
        _boundGrounpAnimation.fillMode =kCAFillModeForwards;
        _boundGrounpAnimation.removedOnCompletion = NO;


    }
    return _boundGrounpAnimation;
}
-(void)animationStart{
    if(self.animationOnce){
        return;
    }
    self.animationOnce = YES;
    [self.handClick setAnimationImages:@[[UIImage imageNamed:@"click-before"],[UIImage imageNamed:@"click-after"]]];
    self.handClick.animationDuration = 1.0f;
    self.handClick.animationRepeatCount = 1;
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    momAnimation.toValue = [NSNumber numberWithFloat:-30];
    momAnimation.duration = 0.5;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    momAnimation.fillMode =kCAFillModeForwards;
    momAnimation.removedOnCompletion = NO;
    momAnimation.delegate =self;
    [momAnimation setValue:@"fi" forKey:@"presentKey"];
    [self.handClick.layer addAnimation:momAnimation forKey:@"transformAnimation"];
}

@end
